package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Storehouse;
import pcjavanet.mobilesell.model.StorehouseExample;
import pcjavanet.mobilesell.model.History; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.HistoryExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.StorehouseMapper;
import pcjavanet.mobilesell.persistence.HistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class HistoryService {


  @Autowired
  private  HistoryMapper historyMapper;
  @Autowired
  private  StorehouseMapper storehouseMapper;


  public int  insert( History history ) throws Exception{
	return historyMapper.insert (history);
  }


  public History  selectByPrimaryKey( History  bean ) {
 	int dbid = bean.getDbid();
	History history = historyMapper.selectByPrimaryKey( dbid );
	Storehouse storehouse = storehouseMapper.selectByPrimaryKey( history.getRefStorehouseDbid() );
	history.setStorehouse(storehouse) ;
	return history ;
  }


  public int  updateByPrimaryKeySelective( History history ) throws Exception{
	return historyMapper.updateByPrimaryKeySelective( history );
  }


  public int  deleteByPrimaryKey( History  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return historyMapper.deleteByPrimaryKey( dbid );
  }


  public List<History>  selectByExample( History history ) throws Exception{
    HistoryExample example = new HistoryExample();
	return historyMapper.selectByExample( example );
  }

  public List<History>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    HistoryExample example = new HistoryExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<History> ls=historyMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> storehouseIdmap = new HashMap<Integer,Integer>();
  		for(History  m : ls){
  			m.setStorehouse(new Storehouse ()) ; 
  			if ( m.getRefStorehouseDbid() != null ) 
  				storehouseIdmap.put(m.getRefStorehouseDbid(), m.getRefStorehouseDbid());
  		};
  		List<Integer> refStorehouseDbids = new ArrayList<Integer>();
  		Iterator<Integer> refStorehouseDbidit = storehouseIdmap.keySet().iterator(); 
  		while ( refStorehouseDbidit.hasNext() ){
  			refStorehouseDbids.add( refStorehouseDbidit.next() );
  		}
  		StorehouseExample storehouseExample = new StorehouseExample();
  		if ( refStorehouseDbids.size() > 0 ) 
  			storehouseExample.createCriteria().andDbidIn( refStorehouseDbids );
  		List<Storehouse> storehouses = storehouseMapper.selectByExample( storehouseExample);
  		for(History m : ls ){
  			for(Storehouse c : storehouses ){
  				if ( m.getRefStorehouseDbid() !=null && c.getDbid() !=null && m.getRefStorehouseDbid().intValue() == c.getDbid().intValue() ) {
  					m.setStorehouse( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    HistoryExample example = new HistoryExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return historyMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( History history , PageBean pageBean ){
    HistoryExample example = new HistoryExample();
	return historyMapper.countByExample( example );
  }
  public List<History> selectByExamplePage( History history , PageBean pageBean ){
  HistoryExample example = new HistoryExample();
	return historyMapper.selectByExamplePage( example , pageBean );
  }

}
